/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-11-27
 * Time: 10:06
 */
public class test1 {
    /*
    NewCoder BM2  链表内指定区间反转
    将一个节点数为 size 链表 m 位置到 n 位置之间的区间反转，

     */
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here
        if(head==null || head.next==null){
            return head;
        }
        ListNode newHead = new ListNode(-1);
        newHead.next = head;
        ListNode tmp = newHead;
        for(int i=1;i<m;i++){
            tmp = tmp.next;
        }
        ListNode left = tmp.next;
        ListNode right = null;
        for(int i =0;i<n-m;i++){
            right = left.next;
            left.next = right.next;
            right.next = tmp.next;
            tmp.next = right;
        }
        return newHead.next;
    }

    public class ListNode {
        int val;
        ListNode next = null;
        public ListNode(int val) {
            this.val = val;
        }
    }
}
